package com.example.SBootDemo.controller.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.example.SBootDemo.model.function.topmodule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.service.Interface.function.topmoduleinterface;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/top")
public class TopModuleController {

	
	@Autowired
	topmoduleinterface topmodule;
	
		 //返回topmodule页面
		 @RequestMapping(value="/getmodules", method=RequestMethod.GET)
		 public String getUsers(HttpServletRequest request,@RequestParam(value="pageinfo", defaultValue="0") int pageinfo) throws Myexception {
			 int flag=topmodule.getModules(pageinfo,request);
			
		     return "/fun/topModule";
		 }
	
	//跳转添加module页面
		 @RequestMapping(value="/addModule", method=RequestMethod.GET)
		 public String toUser(HttpServletRequest request) throws Myexception {
			 topmodule.addpage(request);
		     return "/fun/update/moduleUpdate";
		 }

		 //删除功能
		 @RequestMapping(value="/deleteModule", method=RequestMethod.POST)
		 @ResponseBody
		 public String deleteUser(@RequestBody List<Integer> listid) throws Myexception{
			 
			 topmodule.deleteFun(listid);
		     String message = "删除成功";
		     ObjectMapper mapper = new ObjectMapper();
		   
			 try {
				return mapper.writeValueAsString(message);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		 }
		 //删除功能单个
		 @RequestMapping(value="/deleteModule", method=RequestMethod.GET)
		 public String deleteOneUser(int id) throws Myexception{
			 List<Integer> deleteId = new ArrayList<>();
			 deleteId.add(id);
			 topmodule.deleteFun(deleteId);
			 
			 
			 
			 
			 
			 
			 return "redirect:/top/getmodules";
		 }
//		 
//		 
		 //返回修改模块信息页面
		 @RequestMapping(value="/updateModule", method=RequestMethod.GET)
		 public String toupdateUser(int id,HttpServletRequest request) {
			 topmodule.updatepage(request,id);
			 return "/fun/update/moduleUpdate";
		 }
//		 
		 @RequestMapping(value="/updateModule", method=RequestMethod.POST)
		 public String updateUser(topmodule module,HttpServletRequest request) throws Myexception {
			 topmodule.updateModule(module,request);
		     return "redirect:/top/getmodules";
		 }
		 
}
